﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;

namespace outREACH
{
    public partial class changePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Label lbl1 = (Label)Master.FindControl("Label1");
                lbl1.Text = "Welcome " + Session["Username"].ToString() + "!";
                Label lbl2 = (Label)Master.FindControl("Name");
                Users u = Users.GetName(Session["Username"].ToString());
                lbl2.Text = u.firstName + " " + u.lastName;
                Label lbl3 = (Label)Master.FindControl("Country");
                lbl3.Text = Session["country"].ToString();
                Button btn3 = (Button)Master.FindControl("Button4");
                if (User.IsInRole("admin"))
                {
                    btn3.Visible = true;
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
            Users u1 = Users.getUserProfile(Session["Username"].ToString());
            Image10.ImageUrl = u1.imagePath.ToString();
            Label5.Text = u1.firstName;
            Label6.Text = u1.lastName;
            Label7.Text = u1.country;
            Label8.Text = u1.city;
            Label9.Text = u1.userName;

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Users u = Users.AuthorizeUser(Session["UserName"].ToString());
            if (u.password == TextBox1.Text.ToString())
            {
                if (u.role == "member")
                {
                    if (TextBox2.Text.ToString() == TextBox3.Text.ToString())
                    {
                        Users.changePassword(Session["UserName"].ToString(), TextBox1.Text.ToString(), TextBox3.Text.ToString());
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        TextBox3.Text = "";
                        Label2.Visible = true;
                    }
                    else
                    {
                        Label4.Visible = true;
                    }
                }
                else
                {
                    if (u.role == "NGOs")
                    {
                        if (TextBox2.Text.ToString() == TextBox3.Text.ToString())
                        {
                            Users.changePassword(Session["UserName"].ToString(), TextBox1.Text.ToString(), TextBox3.Text.ToString());
                            NGO.changePassword(Session["UserName"].ToString(), TextBox1.Text.ToString(), TextBox3.Text.ToString());
                            TextBox1.Text = "";
                            TextBox2.Text = "";
                            TextBox3.Text = "";
                            Label2.Visible = true;
                        }
                        else
                        {
                            Label4.Visible = true;
                        }
                    }
                    else
                    {
                        if (u.role == "DC")
                        {
                            if (TextBox2.Text.ToString() == TextBox3.Text.ToString())
                            {
                                Users.changePassword(Session["UserName"].ToString(), TextBox1.Text.ToString(), TextBox3.Text.ToString());
                                DCuser.changePassword(Session["UserName"].ToString(), TextBox1.Text.ToString(), TextBox3.Text.ToString());
                                TextBox1.Text = "";
                                TextBox2.Text = "";
                                TextBox3.Text = "";
                                Label2.Visible = true;
                            }
                            else
                            {
                                Label4.Visible = true;
                            }
                        }
                    }
                    Label3.Visible = true;
                }
            }
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            Users u = Users.getUserProfile(Session["Username"].ToString());
            List<Avatars> results = Avatars.GetAvatars();
            string name = "";
            foreach (Avatars a in results)
            {
                if (a.imagePath.ToString() == Image4.ImageUrl.ToString())
                {
                    name = a.imageName.ToString();
                }
            }
            Users.changeAvatars(u.imagePath.ToString(), u.imageName.ToString(), Image4.ImageUrl, name);
            MedForum.changeAvatars(Image4.ImageUrl, name, Session["Username"].ToString());
            EduForum.changeAvatars(Image4.ImageUrl, name, Session["Username"].ToString());
            MedForumReply.changeAvatars(Image4.ImageUrl, name, Session["Username"].ToString());
            EduForumReply.changeAvatars(Image4.ImageUrl, name, Session["Username"].ToString());
            Panel12.Visible = false;
            Image10.ImageUrl = Image4.ImageUrl.ToString();
            Image image = (Image)Master.FindControl("Image1");
            image.ImageUrl = Image4.ImageUrl.ToString();
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            Users u = Users.getUserProfile(Session["Username"].ToString());
            List<Avatars> results = Avatars.GetAvatars();
            string name = "";
            foreach (Avatars a in results)
            {
                if (a.imagePath.ToString() == Image5.ImageUrl.ToString())
                {
                    name = a.imageName.ToString();
                }
            }
            Users.changeAvatars(u.imagePath.ToString(), u.imageName.ToString(), Image5.ImageUrl, name);
            MedForum.changeAvatars(Image5.ImageUrl, name, Session["Username"].ToString());
            EduForum.changeAvatars(Image5.ImageUrl, name, Session["Username"].ToString());
            MedForumReply.changeAvatars(Image5.ImageUrl, name, Session["Username"].ToString());
            EduForumReply.changeAvatars(Image5.ImageUrl, name, Session["Username"].ToString());
            Panel12.Visible = false;
            Image10.ImageUrl = Image5.ImageUrl.ToString();
            Image image = (Image)Master.FindControl("Image1");
            image.ImageUrl = Image5.ImageUrl.ToString();
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            Users u = Users.getUserProfile(Session["Username"].ToString());
            List<Avatars> results = Avatars.GetAvatars();
            string name = "";
            foreach (Avatars a in results)
            {
                if (a.imagePath.ToString() == Image6.ImageUrl.ToString())
                {
                    name = a.imageName.ToString();
                }
            }
            Users.changeAvatars(u.imagePath.ToString(), u.imageName.ToString(), Image6.ImageUrl, name);
            MedForum.changeAvatars(Image6.ImageUrl, name, Session["Username"].ToString());
            EduForum.changeAvatars(Image6.ImageUrl, name, Session["Username"].ToString());
            MedForumReply.changeAvatars(Image6.ImageUrl, name, Session["Username"].ToString());
            EduForumReply.changeAvatars(Image6.ImageUrl, name, Session["Username"].ToString());
            Panel12.Visible = false;
            Image10.ImageUrl = Image6.ImageUrl.ToString();
            Image image = (Image)Master.FindControl("Image1");
            image.ImageUrl = Image6.ImageUrl.ToString();
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            Users u = Users.getUserProfile(Session["Username"].ToString());
            List<Avatars> results = Avatars.GetAvatars();
            string name = "";
            foreach (Avatars a in results)
            {
                if (a.imagePath.ToString() == Image7.ImageUrl.ToString())
                {
                    name = a.imageName.ToString();
                }
            }
            Users.changeAvatars(u.imagePath.ToString(), u.imageName.ToString(), Image7.ImageUrl, name);
            MedForum.changeAvatars(Image7.ImageUrl, name, Session["Username"].ToString());
            EduForum.changeAvatars(Image7.ImageUrl, name, Session["Username"].ToString());
            MedForumReply.changeAvatars(Image7.ImageUrl, name, Session["Username"].ToString());
            EduForumReply.changeAvatars(Image7.ImageUrl, name, Session["Username"].ToString());
            Panel12.Visible = false;
            Image10.ImageUrl = Image7.ImageUrl.ToString();
            Image image = (Image)Master.FindControl("Image1");
            image.ImageUrl = Image7.ImageUrl.ToString();
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            Users u = Users.getUserProfile(Session["Username"].ToString());
            List<Avatars> results = Avatars.GetAvatars();
            string name = "";
            foreach (Avatars a in results)
            {
                if (a.imagePath.ToString() == Image8.ImageUrl.ToString())
                {
                    name = a.imageName.ToString();
                }
            }
            Users.changeAvatars(u.imagePath.ToString(), u.imageName.ToString(), Image8.ImageUrl, name);
            MedForum.changeAvatars(Image8.ImageUrl, name, Session["Username"].ToString());
            EduForum.changeAvatars(Image8.ImageUrl, name, Session["Username"].ToString());
            MedForumReply.changeAvatars(Image8.ImageUrl, name, Session["Username"].ToString());
            EduForumReply.changeAvatars(Image8.ImageUrl, name, Session["Username"].ToString());
            Panel12.Visible = false;
            Image10.ImageUrl = Image8.ImageUrl.ToString();
            Image image = (Image)Master.FindControl("Image1");
            image.ImageUrl = Image8.ImageUrl.ToString();
        }

        protected void Button12_Click(object sender, EventArgs e)
        {
            Users u = Users.getUserProfile(Session["Username"].ToString());
            List<Avatars> results = Avatars.GetAvatars();
            string name = "";
            foreach (Avatars a in results)
            {
                if (a.imagePath.ToString() == Image9.ImageUrl.ToString())
                {
                    name = a.imageName.ToString();
                }
            }
            Users.changeAvatars(u.imagePath.ToString(), u.imageName.ToString(), Image9.ImageUrl, name);
            MedForum.changeAvatars(Image9.ImageUrl, name, Session["Username"].ToString());
            EduForum.changeAvatars(Image9.ImageUrl, name, Session["Username"].ToString());
            MedForumReply.changeAvatars(Image9.ImageUrl, name, Session["Username"].ToString());
            EduForumReply.changeAvatars(Image9.ImageUrl, name, Session["Username"].ToString());
            Panel12.Visible = false;
            Image10.ImageUrl = Image9.ImageUrl.ToString();
            Image image = (Image)Master.FindControl("Image1");
            image.ImageUrl = Image9.ImageUrl.ToString();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            Panel12.Visible = true;
        }

        protected void Button13_Click(object sender, EventArgs e)
        {
            Panel13.Visible = true;
            Panel14.Visible = false;
        }

        protected void Button15_Click(object sender, EventArgs e)
        {
            Users u = Users.AuthorizeUser(Session["UserName"].ToString());
            if (u.role == "member")
            {
                if (TextBox5.Text.ToString() == TextBox6.Text.ToString())
                {
                    Users.changeEmail(Session["UserName"].ToString(), TextBox6.Text.ToString());
                    TextBox5.Text = "";
                    TextBox6.Text = "";
                    Label12.Visible = true;
                }
                else
                {
                    Label11.Visible = true;
                }
            }
            else
            {
                if (u.role == "NGOs")
                {
                    if (TextBox5.Text.ToString() == TextBox6.Text.ToString())
                    {
                        Users.changeEmail(Session["UserName"].ToString(), TextBox6.Text.ToString());
                        NGO.changeEmail(Session["UserName"].ToString(), u.email, TextBox6.Text.ToString());
                        TextBox5.Text = "";
                        TextBox6.Text = "";
                        Label12.Visible = true;
                    }
                    else
                    {
                        Label11.Visible = true;
                    }
                }
                else
                {
                    if (u.role == "DC")
                    {
                        if (TextBox5.Text.ToString() == TextBox6.Text.ToString())
                        {
                            Users.changeEmail(Session["UserName"].ToString(), TextBox6.Text.ToString());
                            DCuser.changeEmail(Session["UserName"].ToString(), u.email, TextBox6.Text.ToString());
                            TextBox5.Text = "";
                            TextBox2.Text = "";
                            Label12.Visible = true;
                        }
                        else
                        {
                            Label11.Visible = true;
                        }
                    }
                }
            }
        }

        protected void Button16_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            Panel13.Visible = false;
        }

        protected void Button17_Click(object sender, EventArgs e)
        {
            TextBox5.Text = "";
            TextBox6.Text = "";
            Panel14.Visible = false;
        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            Panel13.Visible = false;
            Panel14.Visible = true;
        }
    }
}